## -*- makefile -*-

######################################################################
## C

CC	:= @CC@
CFLAGS	:= @CFLAGS@ @DEFS@ -I@abs_top_srcdir@/../support

LD_SHARED	:= @LD_SHARED@
SLANG_INCLUDE	:= @SLANG_INCLUDE@

######################################################################
## Erlang

ERL	:= @ERL@
ERLC	:= @ERLC@

ERLDIR	:= @ERLDIR@
ERL_C_INCLUDE_DIR := $(ERLDIR)/usr/include

JUNGERL_DIR := @JUNGERL_DIR@

ERLC_FLAGS := -W -I $(JUNGERL_DIR)/lib

ifndef no_debug_info
  ERLC_FLAGS += +debug_info
endif

ifdef debug
  ERLC_FLAGS += -Ddebug
endif

EBIN_DIR := ../ebin
DOC_DIR  := ../doc
EMULATOR := beam

ERL_SOURCES := $(wildcard *.erl)
ERL_HEADERS := $(wildcard *.hrl) $(wildcard ../include/*.hrl)
ERL_OBJECTS := $(ERL_SOURCES:%.erl=$(EBIN_DIR)/%.$(EMULATOR))
ERL_DOCUMENTS := $(ERL_SOURCES:%.erl=$(DOC_DIR)/%.html)

# Hmm, don't know if you are supposed to like this better... ;-)
APPSCRIPT = '$$vsn=shift; $$mods=""; while(@ARGV){ $$_=shift; s/^([A-Z].*)$$/\'\''$$1\'\''/; $$mods.=", " if $$mods; $$mods .= $$_; } while(<>) { s/%VSN%/$$vsn/; s/%MODULES%/$$mods/; print; }'


../ebin/%.app: %.app.src ../vsn.mk Makefile                 
	perl -e $(APPSCRIPT) "$(VSN)" $(MODULES) < $< > $@  

../ebin/%.appup: %.appup
	cp $< $@


$(EBIN_DIR)/%.$(EMULATOR): %.erl
	$(ERLC) $(ERLC_FLAGS) -o $(EBIN_DIR) $<

# generate documentation with edoc:
# this is still not the proper way to do it, but it works
# (see the wumpus application for an example)

$(DOC_DIR)/%.html: %.erl
	${ERL} -noshell \
          -pa ../../syntax_tools/ebin \
          -pa ../../edoc/ebin \
          -pa ../../xmerl/ebin \
	  -pa ../../ucs/ebin \
          -run edoc file $< -run init stop
	mv *.html $(DOC_DIR)

# C linking items

SOCKET_LIBS = @SOCKET_LIBS@

# Miscellaneous

PACKAGE_NAME := @PACKAGE_NAME@
PACKAGE_VERSION := @PACKAGE_VERSION@
INSTALL := @INSTALL@
install_DATA := @INSTALL_DATA@
LN_S := @LN_S@
appdir = $(ERLDIR)/lib/$(PACKAGE_NAME)-$(PACKAGE_VERSION)
beamdir = $(appdir)/ebin
includedir = $(appdir)/include
privdir = $(appdir)/priv
